<?php
/**
 * Created by laoyu.
 * User: hilaoyu@qq.com
 * Date: 2018/5/8
 * Time: 16:32
 */

namespace App\Abstracts\Entities;

use Hilaoyu\LaravelExtend\Entity;

abstract class AbstractEntityVariables extends Entity
{
    static public function get($key,$default=null){

        $_this = new static();

        $model =  $_this->find(function ($query) use ($key){
            return $query->where('key','=',$key) ;
        });

        return data_get($model,'value',$default);
    }


    static public function set($key,$value,$name='',$remarks=''){

        $name = trim($name);

        $_this = new static();

        $model =  $_this->find(function ($query) use ($key){
            return $query->where('key','=',$key) ;
        });

        $data = [
            'key' => $key,
            'value' => $value,
            'name' => $name,
            'remarks' => $remarks,
        ];



        if(!$_this->save($data,$model)){
            static::setStaticError($_this->getError());
            return false;
        }
        return true;
    }

}
